#-------------------------------------------------------------------------------
#	File      : Makefile
#	This file is compiling ARM Cortex-M project.
#
#	Change Logs:
#	Date           Author       Notes
#	2021-08-04     LinuxLife    the first version
#	2024-12-23     LinuxLife    add stlink tools option
#   2025-08-02     LinuxLife    add var CODE_ADDRESS to st-flash
#   2025-10-11     LinuxLife    hide shell log output
#   2025-10-24     LinuxLife    repair link options
#                               add and change comment
#                               add and change shell log output
#-------------------------------------------------------------------------------




#-------------------------------------------------------------------------------
#	Project option
#-------------------------------------------------------------------------------
toolchain   := arm-none-eabi-
target      := stm32
archive     := cortex-m4
float_abi   := soft
fpu         := vfpv4
rom_address := 0x08000000
link_file   := stm32_flash.ld


#-------------------------------------------------------------------------------
#	Toolchain option
#-------------------------------------------------------------------------------
CC      := $(toolchain)gcc
AS      := $(toolchain)as
LD      := $(toolchain)ld
OBJCOPY := $(toolchain)objcopy
OBJDUMP := $(toolchain)objdump
SIZE    := $(toolchain)size


#-------------------------------------------------------------------------------
#	Compiling option
#-------------------------------------------------------------------------------
CC_CONFIG   := -mcpu=$(archive) -mthumb -mfpu=$(fpu) -mfloat-abi=$(float_abi) -O0 -ffunction-sections -fdata-sections
AS_CONFIG   := -mcpu=$(archive) -mthumb -mfpu=$(fpu) -mfloat-abi=$(float_abi) -W -mimplicit-it=thumb 
LD_CONFIG   := -T $(link_file) --gc-sections


#-------------------------------------------------------------------------------
#	source and lib path
#-------------------------------------------------------------------------------
INCDIRS :=  arch \
			bsp \
			module \
			src
								  
SRCDIRS :=  arch \
			bsp \
			module \
			src 

# For example :
# LIBS := module/STenWin/STemWin_CM4.a
LIBS := 


#-------------------------------------------------------------------------------
#	get source file
#   do not edit them
#-------------------------------------------------------------------------------
INCLUDE		:= $(patsubst %, -I %, $(INCDIRS))

#	get file
SFILES		:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.s))
ASMFILES	:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.asm))
CFILES		:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

#	get file path
SFILENDIR	:= $(notdir  $(SFILES))
ASMFILENDIR	:= $(notdir  $(ASMFILES))
CFILENDIR	:= $(notdir  $(CFILES))

#	get object file
SOBJS		:= $(patsubst %, %, $(SFILENDIR:.s=.o))
ASMOBJS		:= $(patsubst %, %, $(ASMFILENDIR:.asm=.o))
COBJS		:= $(patsubst %, %, $(CFILENDIR:.c=.o))
OBJS		:= $(SOBJS) $(COBJS) $(ASMOBJS)

VPATH		:= $(SRCDIRS)

.PHONY: clean


#-------------------------------------------------------------------------------
#	start compiling
#-------------------------------------------------------------------------------
$(target).bin : $(OBJS)
	@echo "linking output files to elf file..."
	@$(LD) $(LD_CONFIG) -o $(target).elf $^ $(LIBS)

	@echo "Generating binary file..."
	@$(OBJCOPY) -O binary -S $(target).elf $@

	@echo "Disassembly elf file..."
	@$(OBJDUMP) -D -m armv7 $(target).elf > $(target).dis

	@echo "all files build Completed!"
	@$(SIZE) -d $(target).elf

$(SOBJS) : %.o : %.s
	@echo "compiling" $<
	@$(AS) $(AS_CONFIG) -o $@ $<

$(ASMOBJS) : %.o : %.asm
	@echo "compiling" $<
	@$(AS) $(AS_CONFIG) -o $@ $<

$(COBJS) : %.o : %.c
	@echo "compiling" $<
	@$(CC) $(CC_CONFIG) $(INCLUDE) -c -o $@ $<


#-------------------------------------------------------------------------------
#	stlink flash to mcu
#-------------------------------------------------------------------------------
flash:
	@st-flash write $(target).bin $(rom_address)

erase:
	@st-flash erase
	
reset:
	@st-flash reset


#-------------------------------------------------------------------------------
#	clean object files
#-------------------------------------------------------------------------------
clean:
	@rm $(target).elf $(target).bin $(target).dis $(OBJS)